import { createStore } from "vuex";

export default createStore({
    state: {
        isLogin: false,
        userData: {
            "role": "",
            "id": 0,
            "username": "",
            "name": ""
        },
        hasBorrow: false
    },
    getters: {
        isLogin(state) {
            return state.isLogin;
        },
        username(state) {
            return state.userData["username"];
        },
        name(state) {
            return state.userData["name"];
        },
        role(state) {
            return state.userData["role"];
        },
        id(state) {
            return state.userData["id"];
        },
        hasBorrow(state) {
            return state.hasBorrow;
        }
    },
    mutations: {
        updateUserData(state, userData) {
            console.log(state);
            if (userData !== null) {
                if (!state.isLogin) {
                    state.isLogin = true;
                }
                state.userData = userData;
            }
        },
        updateUsername(state, username) {
            if (username !== null && state.isLogin) {
                state.userData["username"] = username;
            }
        },
        updateName(state, name) {
            if (name !== null && state.isLogin) {
                state.userData["name"] = name;
            }
        },
        confirmBorrow(state) {
            if (state.isLogin) {
                state.hasBorrow = true;
            }
        },
        confirmReturn(state) {
            if (state.isLogin && state.hasBorrow) {
                state.hasBorrow = false;
            }
        }
    },
    actions: {},
    modules: {}
});
